package com.kun.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kun.dao.BookInfoDao;
import com.kun.dao.OutbookInfoDao;
import com.kun.entity.BookInfo;
import com.kun.entity.OutbookInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OutbookService {
    @Autowired
    private OutbookInfoDao outbookInfoDao;
    @Autowired
    private BookInfoDao bookInfoDao;
    public OutbookInfo findById(Integer outbookId) { return outbookInfoDao.selectByPrimaryKey(outbookId); }
    public Map<String, Object> selectByPage(Integer pageNum, Integer pageSize, OutbookInfo outbookId) {
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
        List<OutbookInfo> list = outbookInfoDao.findAll(outbookId);
        //创建PageInfo对象，并将当前页面数据传入，计算分页相关数据
        PageInfo<OutbookInfo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }
    public int insertOutBook(OutbookInfo outbookInfo, BookInfo bookinfo) {

        bookinfo.setPutbookName(outbookInfo.getOutbookName());
        BookInfo book = bookInfoDao.selectByname(outbookInfo.getOutbookName());
        int num = Integer.valueOf(book.getPutbookNum()).intValue();
        bookinfo.setPutbookId(book.getPutbookId());
        bookinfo.setPutbookNum(Integer.toString(num - outbookInfo.getOutbookNum()));
        bookInfoDao.updateByPrimaryKeySelective(bookinfo);

        return outbookInfoDao.insert(outbookInfo);
    }

    public List<OutbookInfo> exportExcel(OutbookInfo outbookInfo) {
        return outbookInfoDao.findAll(outbookInfo);
    }
    public int deleteOutBook(Integer[] outbookIds){return  outbookInfoDao.batchDelete(outbookIds);}
}
